﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
using System.Data;

namespace DAL
{
    public class AreaService
    {
        public static Area AddArea(Area area)
        {
            string sql = string.Format("insert into Area(Id,Name) values({0},N'{1}');", area.Id, area.Name);
            DBHelper.GetScalar(sql);
            return GetAreaById(area.Id);
        }

        public static Area GetAreaById(int id)
        {
            string sql = string.Format("select * from Area where Id={0}", id);
            return GetAreaBySql(sql);
        }

        public static List<Area> GetAllAreas()
        {
            string sql = string.Format("select * from Area");
            return GetAreasBySql(sql);
        }

        private static Area GetAreaBySql(string sql)
        {
            List<Area> list = GetAreasBySql(sql);
            if (list.Count() > 0)
                return list[0];
            return null;
        }

        private static List<Area> GetAreasBySql(string sql)
        {
            List<Area> list = new List<Area>();
            DataTable dt = DBHelper.GetDataSet(sql);
            foreach (DataRow row in dt.Rows)
            {
                Area area = new Area();
                area.Id = (int)row["Id"];
                area.Name = (string)row["Name"];
                list.Add(area);
            }
            return list;
        }
    }
}
